﻿using System.Collections.Generic;
using System.Linq;
using IGM.DataProvider;
using IGM.Framework.Constants;

namespace IGM.Business
{
    public partial class Business
    {
        #region -- Get Major --

        /// <summary>
        /// This function shows list of Majors.
        /// </summary>
        /// <returns></returns>
        public List<Major> GetListMajor()
        {
            return DataContex.Majors.ToList();
        }

        #endregion

        #region Common

        private static bool MajorCommonValidator(Major major, out string message)
        {
            if (string.IsNullOrWhiteSpace(major.Name))
            {
                message = string.Format(ConstantMessages.RequireFieldIsBlank, "tên Ngành");
                return false;
            }

            if (string.IsNullOrWhiteSpace(major.Code))
            {
                message = string.Format(ConstantMessages.RequireFieldIsBlank, "mã Ngành");
                return false;
            }

            if (major.Name.Length >= 50)
            {
                message = string.Format(ConstantMessages.FieldDataOverMaxLength, "tên Ngành", 50);
                return false;
            }
            message = string.Empty;
            return true;
        }

        #endregion

        #region -- Add Major --

        /// <summary>
        /// 
        /// </summary>
        /// <param name="major"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool AddMajorValidator(Major major, out string message)
        {
            if (!MajorCommonValidator(major, out message)) return false;

            if (DataContex.Majors.Any(f => f.Name.ToLower() == major.Name.ToLower()))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, major.Name);
                return false;
            }

            return true;
        }

        /// <summary>
        /// This function add a new facutly into list.
        /// </summary>
        public void AddMajor(Major major)
        {
            DataContex.Majors.AddObject(major);
            DataContex.SaveChanges();
        }

        #endregion

        #region -- Edit Major --

      
        public bool EditMajorValidator(Major major, out string message)
        {
            var id = major.Id;

            if (!MajorCommonValidator(major, out message)) return false;

            if (DataContex.Majors.Any(m => m.Name.ToLower() == major.Name.ToLower() && m.Id != id))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, major.Name);
                return false;
            }

            if (DataContex.Majors.Any(m => m.Code.ToLower() == major.Code.ToLower() && m.Id != id))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, major.Code);
                return false;
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="major"></param>
        /// <returns></returns>
        public bool EditMajor(Major major)
        {
            var id = major.Id;
            var updateMajor= DataContex.Majors.SingleOrDefault(f => f.Id == id);

            if (updateMajor == null)
                return false;

            updateMajor.Name = major.Name;
            updateMajor.Code = major.Code;

            DataContex.SaveChanges();
            return true;
        }

        #endregion

        #region -- Delete Major --

        public bool DeleteMajorsValidator(IEnumerable<Major> majors, out string message)
        {
            const string intialize = "Ngành: ";
            var itemsCannotIdentify = (from major in majors
                                       let id = major.Id
                                       where !DataContex.Majors.Any(m => m.Id == id)
                                       select major).Aggregate(intialize, (current, major) => current + (major.Name + ", "));

            if (itemsCannotIdentify != intialize)
            {
                message = string.Format(ConstantMessages.SomeItemsCanNotIdentify, itemsCannotIdentify.TrimEnd(','));
                return false;
            }

            var itemsInUse = (from major in majors
                              let id = major.Id
                              where DataContex.Students.Any(m => m.IdMajor == id) || DataContex.Lecturers.Any(m => m.IdMajor == id)
                              select major).Aggregate(intialize, (current, major) => current + (major.Name + ", "));

            if (itemsInUse != intialize)
            {
                message = string.Format(ConstantMessages.SomeItemsInUseCanNotDelete, itemsInUse.TrimEnd(','));
                return false;
            }

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// Delete object
        /// </summary>
        public void DeleteMajors(List<Major> majors)
        {
            for (var i = majors.Count - 1; i >= 0; i--)
            {
                var id = majors[i].Id;
                var deleteMajor = DataContex.Majors.Single(m => m.Id == id);
                DataContex.Majors.DeleteObject(deleteMajor);
            }
            DataContex.SaveChanges();
        }

        #endregion
       
    }
}